* this do-file constructs the .dta file necessary for the citations lag model only with US as the cited country and more citing countries

set more off
cd d:\data

use pat80_11.dta, clear

keep patent gyear gdate country assignee cat subcat asscode

ren patent citing
so citing
mer 1:n citing using cite80_11.dta
tab _m
* _m=1 are patents that never cited any other patent
* _m=2 are patents that were issued in some other period than 1980-2011
* count if _m==2&gyear>=1980&gyear<=2011
keep if _m==3
so cited gdate
qui by cited:gen cite_order = _n

su cite_order,d
drop _m

keep if cite_order<=2

ren gdate   date_cited
ren gyear   year_cited
drop citing
so cited
sa temp.dta,replace


use pat80_11.dta, clear
ren country cited_country
ren cat     cited_cat
ren subcat  cited_subcat
ren assignee cited_assignee
ren asscode  cited_asscode
keep patent gdate gyear cited*
gen cited = patent
so cited

mer 1:n cited using temp.dta
tab _m
drop if _m==2
drop cited _m

replace cite_order = 1 if cite_order==.

reshape wide date_cited year_cited country assignee cat subcat asscode,j(cite_order) i(patent)

gen y1 = date_cited1 - gdate
gen y2 = date_cited2 - date_cited1


gen dt1 = (y1~=.)
gen dt2 = (y2~=.)


gen sc1 = (assignee1==cited_assignee)&(cited_asscode~=1)&(cited_asscode~=4)&(cited_asscode~=5)
gen sc2 = (assignee2==cited_assignee)&(cited_asscode~=1)&(cited_asscode~=4)&(cited_asscode~=5)


gen samecat1 = (cited_subcat==subcat1)
gen samecat2 = (cited_subcat==subcat2)


gen corp1 = (asscode1==2)|(asscode1==3)
gen corp2 = (asscode2==2)|(asscode2==3)




keep if cited_country=="US"

egen temp = max(gdate)
gen c = temp-gdate
drop temp

replace y1 = c    if y1==.
replace y2 = c-y1 if y2==.


drop if y1<0
drop if y2<0


ren cat1 citing_cat1
ren cat2 citing_cat2

tab citing_cat1, gen(c1_)
tab citing_cat2, gen(c2_)

ren samecat1 cat1
ren samecat2 cat2


sa data_allyears_usonly,replace

ren cited_country country

replace country="RU" if country=="RU"|country=="GA"|country=="UA"|country=="MD"|country=="BY"|country=="AM"|country=="AZ"|country=="KZ"|country=="UZ"|country=="KZ"|country=="EE"|country=="LV"|country=="LT"|country=="SU"  

ren country cited_country

forvalues i=1(1)2 {
  ren country`i' country
  replace country="RU" if country=="RU"|country=="GA"|country=="UA"|country=="MD"|country=="BY"|country=="AM"|country=="AZ"|country=="KZ"|country=="UZ"|country=="KZ"|country=="EE"|country=="LV"|country=="LT"|country=="SU"  
  ren country country`i'
}


gen DE = (cited_country=="DE")
gen FR = (cited_country=="FR")
gen GB = (cited_country=="GB")
gen EU = (cited_country=="BE"|cited_country=="DK"|cited_country=="GR"|cited_country=="ES"|cited_country=="IE"|cited_country=="IT"|cited_country=="LU"|cited_country=="NL"|cited_country=="AT"|cited_country=="PT"|cited_country=="FI"|cited_country=="SE")
gen US = (cited_country=="US")
gen JP = (cited_country=="JP")
gen KR = (cited_country=="KR")
gen TW = (cited_country=="TW")
gen CN = (cited_country=="CN")
gen AU = (cited_country=="AU") 
gen CA = (cited_country=="CA") 
gen CH = (cited_country=="CH") 
gen IL = (cited_country=="IL") 
gen IN = (cited_country=="IN") 
gen RU = (cited_country=="RU") 
gen RW = (DE==0&FR==0&GB==0&JP==0&EU==0&US==0&KR==0&TW==0&CN==0&AU==0&CA==0&CH==0&IL==0&IN==0&RU==0)

gen DEc1 = (country1=="DE")
gen FRc1 = (country1=="FR")
gen GBc1 = (country1=="GB")
gen EUc1 = (country1=="BE"|country1=="DK"|country1=="GR"|country1=="ES"|country1=="IE"|country1=="IT"|country1=="LU"|country1=="NL"|country1=="AT"|country1=="PT"|country1=="FI"|country1=="SE")
gen USc1 = (country1=="US")
gen JPc1 = (country1=="JP")
gen KRc1 = (country1=="KR")
gen TWc1 = (country1=="TW")
gen CNc1 = (country1=="CN")
gen AUc1 = (country1=="AU") 
gen CAc1 = (country1=="CA") 
gen CHc1 = (country1=="CH") 
gen ILc1 = (country1=="IL") 
gen INc1 = (country1=="IN") 
gen RUc1 = (country1=="RU") 
gen RWc1 = (DEc1==0&FRc1==0&GBc1==0&JPc1==0&EUc1==0&USc1==0&KRc1==0&TWc1==0&CNc1==0&AUc1==0&CAc1==0&CHc1==0&ILc1==0&INc1==0&RUc1==0)



gen DEc2 = (country2=="DE")
gen FRc2 = (country2=="FR")
gen GBc2 = (country2=="GB")
gen EUc2 = (country2=="BE"|country2=="DK"|country2=="GR"|country2=="ES"|country2=="IE"|country2=="IT"|country2=="LU"|country2=="NL"|country2=="AT"|country2=="PT"|country2=="FI"|country2=="SE")
gen USc2 = (country2=="US")
gen JPc2 = (country2=="JP")
gen KRc2 = (country2=="KR")
gen TWc2 = (country2=="TW")
gen CNc2 = (country2=="CN")
gen AUc2 = (country2=="AU") 
gen CAc2 = (country2=="CA") 
gen CHc2 = (country2=="CH") 
gen ILc2 = (country2=="IL") 
gen INc2 = (country2=="IN") 
gen RUc2 = (country2=="RU") 
gen RWc2 = (DEc2==0&FRc2==0&GBc2==0&JPc2==0&EUc2==0&USc2==0&KRc2==0&TWc2==0&CNc2==0&AUc2==0&CAc2==0&CHc2==0&ILc2==0&INc2==0&RUc2==0)
   





    
sa data_allyears_usonly, replace

u data_allyears_usonly,clear

ren gyear temp

forvalues i=1(1)2 {
  ren country`i' country
  ren subcat`i'  subcat
  ren year_cited`i' gyear

  so country subcat gyear
  mer n:1 country subcat gyear using base_cst
  ren b_cst b_cst`i'
  drop if _m==2
  drop _m

  ren  country country`i'
  ren  subcat subcat`i'
  ren  gyear year_cited`i'

}
ren temp gyear

recode gyear (1980/1983=1) (1984/1987=2) (1988/1991=3) (1992/1995=4) (1996/1999=5) (2000/2003=6) (2004/2007=7) (2008/2011=8), gen(p)

sa temp, replace

u temp, clear

keep c gyear y1 y2 dt1 dt2 sc1 sc2 cat1 cat2 corp1 corp2 DE* FR* GB* JP* EU* US* KR* TW* CN* AU* CA* CH* IL* IN* RU* RW*  b_cst1 b_cst2 p

replace RWc1 = RWc1 + CNc1 + INc1 if p==1|p==2
replace RWc2 = RWc2 + CNc2 + INc2 if p==1|p==2

sa temp, replace

local i=1
while `i'<=2 {
    u temp, clear
    keep if p==`i'
    order    c gyear y1 y2 dt1 dt2 sc1 sc2 cat1 cat2 corp1 corp2 DEc1 FRc1 GBc1 JPc1 EUc1 USc1 KRc1 TWc1 AUc1 CAc1 CHc1 ILc1 RUc1 RWc1 DEc2 FRc2 GBc2 JPc2 EUc2 USc2 KRc2 TWc2 AUc2 CAc2 CHc2 ILc2 RUc2 RWc2 b_cst1 b_cst2 
    outfile  c gyear y1 y2 dt1 dt2 sc1 sc2 cat1 cat2 corp1 corp2 DEc1 FRc1 GBc1 JPc1 EUc1 USc1 KRc1 TWc1 AUc1 CAc1 CHc1 ILc1 RUc1 RWc1 DEc2 FRc2 GBc2 JPc2 EUc2 USc2 KRc2 TWc2 AUc2 CAc2 CHc2 ILc2 RUc2 RWc2 b_cst1 b_cst2                using usonly`i'.raw, wide replace
    local i = `i'+1
}

local i=3
while `i'<=8 {
    u temp, clear
    keep if p==`i'
    order    c gyear y1 y2 dt1 dt2 sc1 sc2 cat1 cat2 corp1 corp2 DEc1 FRc1 GBc1 JPc1 EUc1 USc1 KRc1 TWc1 CNc1 AUc1 CAc1 CHc1 ILc1 INc1 RUc1 RWc1 DEc2 FRc2 GBc2 JPc2 EUc2 USc2 KRc2 TWc2 CNc2 AUc2 CAc2 CHc2 ILc2 INc2 RUc2 RWc2 b_cst1 b_cst2 
    outfile  c gyear y1 y2 dt1 dt2 sc1 sc2 cat1 cat2 corp1 corp2 DEc1 FRc1 GBc1 JPc1 EUc1 USc1 KRc1 TWc1 CNc1 AUc1 CAc1 CHc1 ILc1 INc1 RUc1 RWc1 DEc2 FRc2 GBc2 JPc2 EUc2 USc2 KRc2 TWc2 CNc2 AUc2 CAc2 CHc2 ILc2 INc2 RUc2 RWc2 b_cst1 b_cst2                using usonly`i'.raw, wide replace
    local i = `i'+1
}

